﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using DG.Tweening;
using UnityEngine.UI;

public class LevelDragObject : MonoBehaviour
{
    public Rigidbody2D r2d;
    private float curSpeedY;
    private bool onGround = false;
    private int enterCount = 0;
    public Text loveText;
    public Button loveButton;
    public GameLevelItem gameLevelItem;

    private void Start()
    {
        loveButton.onClick.AddListener(() =>
        {
            loveText.color = Color.black;
            gameLevelItem.TriggerResult(true);
        });
    }

    private void OnCollisionEnter2D(Collision2D collision)
    {
        //Debug.Log("c name:" + collision.gameObject.name);
        //Debug.Log("c speed：" + collision.rigidbody.velocity.y);
        //Debug.Log("c cur speed：" + curSpeedY);
        collision.gameObject.transform.parent = this.gameObject.transform;
        if (enterCount != 0)
            SoundManager.Instance.PlayAudio(SoundType.Fall.ToString(), false);
        if (Mathf.Abs(curSpeedY) > 250)
            this.transform.DOLocalMoveY(this.transform.localPosition.y - 200, 2.2f);
        onGround = true;
        enterCount++;
    }

    private void OnCollisionExit2D(Collision2D collision)
    {
        onGround = false;
        collision.gameObject.transform.parent = this.gameObject.transform.parent;
    }

    private void LateUpdate()
    {
        if (!onGround)
        {
            if (r2d.velocity.y != 0)
                curSpeedY = r2d.velocity.y;
        }
    }

}
